package main

import (
	"github.com/spf13/viper"
	"log"
)

func initConfig() {
	viper.SetConfigFile("./config.yaml")

	err := viper.ReadInConfig()
	if err != nil {
		viper.Set("PROXY_SERVER", ":8053")
		viper.Set("DNS_SERVER", ":8052")
		viper.Set("HOSTS_FILE", "/etc/hosts")
		viper.Set("LOG_LEVEL", "fatal")
		err := viper.WriteConfig()
		if err != nil {
			log.Fatal(err)
			return
		}
		log.Printf("写入新配置文件, 可以按需修改: config.yaml")
		return
	}

	dns := viper.GetString(dnsKey)
	if len(dns) > 0 {
		dnsServer = dns
	}
	proxy := viper.GetString(proxyKey)
	if len(proxy) > 0 {
		proxyServer = proxy
	}
	hosts := viper.GetString(hostsFileKey)
	if len(hosts) > 0 {
		hostsFile = hosts
	}
	logLvl := viper.GetString(logLevelKey)
	if len(logLvl) > 0 {
		logLevel = logLvl
	}

	log.Printf("从配置文件读取: config.ini")
}
